package com.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.common.domain.entity.ResponseResult;
import com.common.domain.entity.VisitRecord;
import com.common.domain.vo.VisitRecordVo;
import com.common.mapper.VisitRecordMapper;
import com.common.service.VisitRecordService;
import com.common.util.FileEncodeUtil;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
* @author pc
* @description 针对表【sys_visit_record】的数据库操作Service实现
* @createDate 2023-11-02 21:08:46
*/
@Service
public class VisitRecordServiceImpl extends ServiceImpl<VisitRecordMapper, VisitRecord>
implements VisitRecordService {

    @Autowired
    private VisitRecordMapper visitRecordMapper;

    @Override
    public void insetData(VisitRecord visitRecord) {
        save(visitRecord);
    }

    @Override
    public ResponseResult getVisitRecode(Long id) {
        LambdaQueryWrapper<VisitRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VisitRecord::getConsultationId, id);
        VisitRecord one = getOne(queryWrapper);
        return ResponseResult.okResult(one);
    }

    @Override
    public VisitRecordVo getUserVisitRecode(Long id) {
        return visitRecordMapper.getUserVisitRecode(id);
    }

    @Override
    public ResponseResult downloadPdf(InputStream inputStream, VisitRecordVo visitRecordVo, HttpServletResponse response) throws IOException {
        PdfReader reader;
        ByteArrayOutputStream bos = null;
        PdfStamper stamper;
        reader = new PdfReader(inputStream);

        try {
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            //设置字体
            final BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            ArrayList<BaseFont> fontList = new ArrayList<>();
            fontList.add(font);

            AcroFields form = stamper.getAcroFields();
            form.setSubstitutionFonts(fontList);
            form.setField("doctorName",visitRecordVo.getDoctorName());
            form.setField("userName",visitRecordVo.getUserName());
            form.setField("appointmentDate",visitRecordVo.getAppointmentDate());
            form.setField("slot",visitRecordVo.getSlot());
            form.setField("describe",visitRecordVo.getDescribe());
            form.setField("drugInformation",visitRecordVo.getDrugInformation());
            FileEncodeUtil.insertImage(form, stamper, "image", visitRecordVo.getFilePath());

            stamper.setFormFlattening(true);
            stamper.close();
            reader.close();

            String encode = FileEncodeUtil.encode(bos.toByteArray());

            FileEncodeUtil.writePdf(encode,visitRecordVo.getUserName()+"_"+visitRecordVo.getAppointmentDate()+"_就诊单信息",response);
            return ResponseResult.okResult();

        } catch (Exception e) {
            log.error("报错信息:"+e,e);
        }finally {
            return ResponseResult.okResult();
        }
    }
}
